from django.shortcuts import render

# Create your views here.
from django.shortcuts import render

# Create your views here.
from rest_framework.viewsets import ModelViewSet
from .gathering_paginations import MyPage
from rest_framework.pagination import PageNumberPagination
from .gathering_serializers import *
from rest_framework.generics import ListAPIView
from rest_framework.views import APIView
from .models import Gathering
from user.models import User
from rest_framework.response import Response

#活动列表
class GatheringListView(ListAPIView):
    queryset = Gathering.objects.all()
    serializer_class = GatheringListSerializer
    pagination_class = MyPage

#活动详情
class GatheringDetailView(ModelViewSet):
    queryset = Gathering.objects.all()
    serializer_class = GatheringSerializer


#参加活动,对manytomanyfield字段进行增删改查，也可以用APIView视图直接增删改查，用序列化器比较麻烦，
class GatheringlView(APIView):

    def post(self, request, pk):
        activity = Gathering.objects.get(pk=pk)
        user = request.user
        # user = User.objects.get(id=1)
        activity.users.add(user) # 网中间表插入数据

        return Response({'message':'参与成功','success':True})
